package com.app.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.MenuRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.app.R;
import com.app.http.HttpDataApi;
import com.app.view.AbsToolbar;
import com.app.view.DefaultToolbar;
import com.app.view.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

public abstract class ActivityBaseCompat extends AppCompatActivity implements AbsToolbar.OnNavigationClickListener, HttpDataApi.OnRequestCallback {

    private LinearLayout mContentView;
    protected AbsToolbar mToolbar;
    private Toolbar.OnMenuItemClickListener onMenuItemClickListener;
    protected OnBackPressedListener onBackPressedListener;



    // 用于页面显示前的Loading页面
    private FrameLayout mLoadingLayout;
    // 用于显示 Loading Dialog
    public LoadingDialog mDialog;


    public interface OnBackPressedListener {
        boolean onBackPressed();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        mContentView = new LinearLayout(this);
        mContentView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mContentView.setOrientation(LinearLayout.VERTICAL);
        mToolbar = toolbar();
        if (null != mToolbar) {
            ViewGroup.LayoutParams layoutParams = mToolbar.getLayoutParams();
            if (null == layoutParams) {
                layoutParams = generateDefaultLayoutParams();
            }
            mToolbar.setOnNavigationClickListener(this);
            mContentView.addView(mToolbar, layoutParams);
        }
        super.setContentView(mContentView);
    }


    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getLayoutInflater().inflate(layoutResID, mContentView);
    }

    @Override
    public void setContentView(View view) {
        setContentView(view, view.getLayoutParams());
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (null == params) {
            params = generateDefaultLayoutParams();
        }
        if (mContentView.indexOfChild(view) == -1) {
            mContentView.addView(view, params);
        }
    }

    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (null != mToolbar) {
            mToolbar.setTitle(title);
        }
    }

    public void setTitleClickListener(View.OnClickListener listener) {
        if (null != mToolbar) {
            mToolbar.setTitleClickListener(listener);
        }
    }

    public void setNavigationDrawable(int drawableId) {
        if (null != mToolbar) {
            mToolbar.setNavigationDrawable(drawableId);
        }
    }

    public void setTitleColor(int colorId) {
        if (null != mToolbar) {
            mToolbar.setTitleColor(colorId);
        }
    }

    public void setToolbarBackgroundColor(@ColorRes int colorId) {
        if (null != mToolbar) {
            mToolbar.setBackgroundColor(getResources().getColor(colorId));
        }
    }

    public void inflateMenu(@MenuRes int menuId) {
        clearMenu();
        if (null != mToolbar) {
            mToolbar.inflateMenu(menuId);
            mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (null != onMenuItemClickListener) {
                        onMenuItemClickListener.onMenuItemClick(item);
                    }
                    ActivityBaseCompat.this.onMenuItemClick(item);
                    return true;
                }
            });
            // Ugly code... But i can't help...
            Menu menu = mToolbar.getMenu();
            if (null != menu) {
                for (int i = 0; i < menu.size(); i++) {
                    MenuItem item = menu.getItem(i);
                    SpannableString text = new SpannableString(item.getTitle() + "");
                    text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);
                    item.setTitle(text);
                }
            }
        }
    }

    public void clearMenu() {
        if (null != mToolbar) {
            mToolbar.getMenu().clear();
        }
    }

    /**
     * 获取Toolbar实例,该方法可以被重写
     *
     * @return
     */
    public AbsToolbar toolbar() {
        if (null == mToolbar) {
            mToolbar = new DefaultToolbar(this);
        }
        return mToolbar;
    }

    /**
     * 移除左端返回按钮
     */
    public void removemNavigationView() {
        if (mToolbar == null) return;
        mToolbar.removeViewAt(0);
    }




    @Override
    public void onNavigationClick(View view) {
        finish();
    }

    public void setOnMenuItemClickListener(final Toolbar.OnMenuItemClickListener onMenuItemClickListener) {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    protected void onMenuItemClick(MenuItem item) {
    }


    protected boolean onNeedCallSuper(String url) {
        return true;
    }

    public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
        this.onBackPressedListener = onBackPressedListener;
    }
    @Override
    public void onBackPressed() {
        if(null != onBackPressedListener && onBackPressedListener.onBackPressed()) return;
        super.onBackPressed();
    }

    /**
     * 显示加载页面
     *
     * @return 当前的对话框对象
     */
    public void showLoadingDialog() {
        showLoadingDialog("请稍等...");
    }

    /**
     * 显示加载页面
     *
     * @param loadingText 加载提示文字
     * @return 当前的对话框对象
     */
    public void showLoadingDialog(String loadingText) {
        if (mDialog == null) {
            mDialog = new LoadingDialog(this);
        }
        mDialog.setTitle(loadingText);
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
        mDialog.show();
    }

    public void dissmissLoadingDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    public void loadingStarted() {
        if (null == mLoadingLayout) {
            mLoadingLayout = new FrameLayout(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.gravity = Gravity.CENTER;
            mLoadingLayout.setLayoutParams(lp);
            getLayoutInflater().inflate(R.layout.progress_bar_new, mLoadingLayout);
        }
        for (int i = 0; i < mContentView.getChildCount(); i++) {
            if (mContentView.getChildAt(i) != mToolbar) {
                mContentView.getChildAt(i).setVisibility(View.GONE);
            }
        }
        if (mContentView.indexOfChild(mLoadingLayout) == -1) {
            mLoadingLayout.setVisibility(View.VISIBLE);
            mContentView.addView(mLoadingLayout);
        }
    }

    public void loadingFinished() {
        if (null != mLoadingLayout && mContentView.indexOfChild(mLoadingLayout) != -1) {
            mContentView.removeView(mLoadingLayout);
            for (int i = 0; i < mContentView.getChildCount(); i++) {
                mContentView.getChildAt(i).setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }


}
